package com.nbsaas.boot.contoller;

import com.nbsaas.boot.app.api.apis.AppMenuApi;
import com.nbsaas.boot.app.api.domain.request.AppMenuRequest;
import com.nbsaas.boot.app.api.domain.request.AppMenuSearch;
import com.nbsaas.boot.app.api.domain.response.AppMenuResponse;
import com.nbsaas.boot.app.api.domain.simple.AppMenuSimple;
import com.nbsaas.boot.app.ext.apis.AppMenuExtApi;
import com.nbsaas.boot.app.ext.apis.AppMenuTenantExtApi;
import com.nbsaas.boot.app.ext.domain.request.MenuDragRequest;
import com.nbsaas.boot.app.ext.domain.request.UpdateRoleMenuRequest;
import com.nbsaas.boot.app.ext.domain.simple.AppMenuExtSimple;
import com.nbsaas.boot.rest.annotations.*;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.rest.response.PageResponse;
import com.nbsaas.boot.rest.response.ResponseObject;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 *  对外控制器
 */
@RequiresAuthentication
@RestController
@RequestMapping("/appMenu")
public class AppMenuController {


    @Resource
    private AppMenuApi appMenuApi;

    @Resource
    private AppMenuExtApi appMenuExtApi;


    @Resource
    private AppMenuTenantExtApi appMenuTenantExtApi;

    @RequiresPermissions("appMenu")
    @RequestMapping("/tree")
    public ListResponse<AppMenuExtSimple> tree(Long appId, Long userId, Long tenant) {
        return appMenuExtApi.tree(appId, userId, tenant);
    }

    @RequiresPermissions("appMenu")
    @RequestMapping("/root")
    public ListResponse<AppMenuExtSimple> root(Long appId) {
        return appMenuExtApi.root(appId);
    }

    @RequiresPermissions("appMenu")
    @RequestMapping("/rootTenant")
    public ListResponse<AppMenuExtSimple> rootTenant(Long appId, Long tenantId) {
        return appMenuTenantExtApi.root(tenantId, appId, true);
    }

    @RequiresPermissions("appMenu")
    @RequestMapping("/rootTenantAll")
    public ListResponse<AppMenuExtSimple> rootTenantAll(Long appId, Long tenantId) {
        return appMenuTenantExtApi.root(tenantId, appId, false);
    }

    @RequiresPermissions("appMenu")
    @RequestMapping("/findByRole")
    public ListResponse<AppMenuExtSimple> findByRole(Long appId, Long role) {
        return appMenuExtApi.findByRole(appId, role);
    }

    @RequiresPermissions("appMenu")
    @RequestMapping("/selectForPermission")
    public ListResponse<Long> selectForPermission(Long appId, Long role) {
        return appMenuExtApi.selectForPermission(appId, role);
    }

    @RequiresPermissions("appMenu")
    @RequestMapping("/permissions")
    public List<String> permissions(Long appId, Long role) {
        return appMenuExtApi.permissions(appId, role);
    }

    @RequiresPermissions("appMenu")
    @RequestMapping("/selectPermissionByUser")
    public ListResponse<String> selectPermissionByUser(Long appId, Long userId, Long tenant) {
        return appMenuExtApi.selectPermissionByUser(appId, userId, tenant);
    }

    @RequiresPermissions("appMenu")
    @RequestMapping("/updateRoleMenus")
    public ResponseObject<?> updateRoleMenus(@RequestBody UpdateRoleMenuRequest request) {
        return appMenuExtApi.updateRoleMenus(request);
    }

    @RequiresPermissions("appMenu")
    @RequestMapping("/drag")
    public ResponseObject<?> drag(@RequestBody MenuDragRequest request) {
        return appMenuExtApi.drag(request);
    }


    @RequiresPermissions("appMenu")
    @RequestMapping("/search")
    public PageResponse<AppMenuSimple> search(@RequestBody AppMenuSearch request) {
        return appMenuApi.search(request);
    }

    @RequiresPermissions("appMenu")
    @RequestMapping("/list")
    public ListResponse<AppMenuSimple> list(@RequestBody AppMenuSearch request) {
        return appMenuApi.list(request);
    }

    /**
     * 添加数据
     *
     * @param request
     * @return
     */
    @RequiresPermissions("appMenu")
    @CreateData
    @RequestMapping("/create")
    public ResponseObject<AppMenuResponse> create(@RequestBody @Validated(AddOperator.class) AppMenuRequest request) {
        return appMenuApi.create(request);
    }

    @RequiresPermissions("appMenu")
    @UpdateData
    @RequestMapping("/update")
    public ResponseObject<AppMenuResponse> update(@RequestBody @Validated(UpdateOperator.class) AppMenuRequest request) {
        return appMenuApi.update(request);
    }

    @RequiresPermissions("appMenu")
    @RequestMapping("/delete")
    public ResponseObject<?> delete(@RequestBody @Validated(DeleteOperator.class) AppMenuRequest request) {
        return appMenuApi.delete(request);
    }

    @RequiresPermissions("appMenu")
    @RequestMapping("/view")
    public ResponseObject<AppMenuResponse> view(@RequestBody @Validated(ViewOperator.class) AppMenuRequest request) {
        return appMenuApi.view(request);
    }
}